package com.example.myprogressbar;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

    private ProgressBar progressBar;
    private ProgressBar progressBar2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取控件元素
        progressBar = findViewById(R.id.pb);
        progressBar2 = findViewById(R.id.pb2);

        Button pbBtn = findViewById(R.id.pb_btn);
        Button pb2Btn = findViewById(R.id.pb2_btn);

        // 给按钮添加监听
        pbBtn.setOnClickListener(v -> {
            if (progressBar.getVisibility() == View.GONE) { // 当前是隐藏的
                // 设置进度条状态为显示
                progressBar.setVisibility(View.VISIBLE);
            } else {
                // 设置进度条状态为隐藏
                progressBar.setVisibility(View.GONE);
            }
        });

        // 给按钮添加监听
        pb2Btn.setOnClickListener(v -> {
            int progress = progressBar2.getProgress();
            if (progress < 100) {
                progress += 10;
                progressBar2.setProgress(progress);
            } else {
                progressBar2.setProgress(10);
            }
        });

    }
}